﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BaseText;
using Srvtools;

public partial class ServicesSheet_OfficesPullDown : System.Web.UI.Page
{
    private Srvtools.WebDataSet WMaster;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            NewDataSet();


            MasterPage_MasterPage master = (MasterPage_MasterPage)this.Page.Master;
            master.HelpHref = "~/Help/ServicesSheet/办公用品领用管理.htm";
            master.PageName = "办公领用管理";
            master.PicUrl = "";
            master.IntiBar();
        }
    }

    private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ServicesSheet_OfficesPullDown));
        this.WMaster = new Srvtools.WebDataSet();
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).BeginInit();
        // 
        // WMaster
        // 
        this.WMaster.Active = true;
        this.WMaster.AlwaysClose = false;
        this.WMaster.DeleteIncomplete = true;
        this.WMaster.Guid = null;
        this.WMaster.LastKeyValues = null;
        this.WMaster.Locale = new System.Globalization.CultureInfo("zh-CN");
        this.WMaster.PacketRecords = 100;
        this.WMaster.Position = -1;
        this.WMaster.RemoteName = "ServiceSheet.View_Services_OfficeSupplies";
        this.WMaster.ServerModify = false;
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).EndInit();

    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        NewDataSet();
    }

    //protected void btnClear_Click(object sender, EventArgs e)
    //{
    //    this.QueryEmp.BindingValue = "";
    //    this.QueryEmp.BindingText = "";

    //    this.DepartmentDList1.SelectDepmanetId = "0";
    //    this.DSelYearMonth1.SelectYear = "0";
    //    this.DSelYearMonth1.SelectMonth = "0";

    //    NewDataSet();
    //}

    public String GetYearMonth(object InYear, object InMonth)
    {
        int IY = (int)InYear;
        int IM = (int)InMonth;
        return IY.ToString() + "/" + IM.ToString();
    }

    private void NewDataSet()
    {
        DataSet Tmp;
        string Sql = "";

        string strEmpId = SelUser.EmpName;
        string strDep = this.DepartmentDList1.SelectDepmanetId;
        string strYear = this.DSelYearMonth1.SelectYear;
        string strMonth = this.DSelYearMonth1.SelectMonth;

        InitializeComponent();


        Sql = Sql + "  SELECT Services_OfficeSupplies.SuppliesId, Services_OfficeSupplies.SuppliesEmpId, ";
        Sql = Sql + " Services_OfficeSupplies.SuppliesYear, Services_OfficeSupplies.SuppliesMonth, ";
        Sql = Sql + "  Services_DetailsSupplies.DetailsId, Services_DetailsSupplies.SId, ";
        Sql = Sql + " Services_SV_Supplies.SType,Services_DetailsSupplies.Counts,";//2008-11-6修改。删除Services_DetailsSupplies.SType,改为Services_SV_Supplies.SType,
        Sql = Sql + " Services_DetailsSupplies.IsConfirm,";//2008-11-6添加
        Sql = Sql + " Services_DetailsSupplies.UnitPrice,";//2009-04-15新增
        Sql = Sql + "  (SELECT Services_OfficeSupplies_Stock.StockCount ";
        Sql = Sql + "  FROM Services_OfficeSupplies_Stock ";
        Sql = Sql + "  WHERE (Services_OfficeSupplies_Stock.Operate = 0) AND ";
        Sql = Sql + "  (Services_OfficeSupplies_Stock.SuppliesId = Services_OfficeSupplies.SuppliesId) ";
        Sql = Sql + "  AND (Services_OfficeSupplies_Stock.DetailsId = Services_DetailsSupplies.DetailsId))";
        Sql = Sql + "  AS CCounts, Services_SV_Supplies.SName, Employees.EmpName, ";
        Sql = Sql + "  Employees.DepartmentId, Department.DepartmentName ";
        Sql = Sql + "  FROM Services_OfficeSupplies INNER JOIN ";
        Sql = Sql + "   Services_DetailsSupplies ON  ";
        Sql = Sql + "   Services_OfficeSupplies.SuppliesId = Services_DetailsSupplies.SuppliesId INNER JOIN ";
        Sql = Sql + "  Employees ON ";
        Sql = Sql + "  Services_OfficeSupplies.SuppliesEmpId = Employees.EmployeeID INNER JOIN ";
        Sql = Sql + "  Services_SV_Supplies ON ";
        Sql = Sql + "  Services_DetailsSupplies.SId = Services_SV_Supplies.SId INNER JOIN ";
        Sql = Sql + "  Department ON Employees.DepartmentId = Department.DepartmentId ";
        Sql = Sql + "  GROUP BY Services_OfficeSupplies.SuppliesId, Services_OfficeSupplies.SuppliesEmpId, ";
        Sql = Sql + "   Services_OfficeSupplies.SuppliesYear, Services_OfficeSupplies.SuppliesMonth, ";
        Sql = Sql + "   Services_OfficeSupplies.FlowFlag, Services_DetailsSupplies.DetailsId, ";
        Sql = Sql + "   Services_DetailsSupplies.SId, Services_SV_Supplies.SType,";//Services_DetailsSupplies.SType, ";//2008-11-6修改。删除Services_DetailsSupplies.SType改为Services_SV_Supplies.SType,
        Sql = Sql + "  Services_DetailsSupplies.Counts,Services_DetailsSupplies.IsConfirm, Services_DetailsSupplies.UnitPrice,Services_SV_Supplies.SName, ";//2008-11-6添加,对应上面的。
        Sql = Sql + "  Employees.EmpName, Employees.DepartmentId, ";
        Sql = Sql + "  Department.DepartmentName ";
        Sql = Sql + " HAVING (Services_OfficeSupplies.FlowFlag = 'Z')";

        if (strEmpId != "" && strEmpId != string.Empty) Sql = Sql + " AND Employees.EmpName like '%" + strEmpId + "%' ";
        if (strDep != "" && strDep != string.Empty && strDep != "0") Sql = Sql + " AND Employees.DepartmentId Like '" + strDep + "%'";
        if (strYear != "" && strYear != "0" && strYear != string.Empty) Sql = Sql + string.Format(" AND Services_OfficeSupplies.SuppliesYear ={0}  ", strYear, strMonth);
        if (strMonth != "" && strMonth != string.Empty && strMonth != "0") Sql = Sql + string.Format(" And Services_OfficeSupplies.SuppliesMonth={1} ", strYear, strMonth);

        Tmp = WMaster.Execute(Sql);

        DataTable svTable;
        svTable = MakeNewWTable();
        DataRow SR;
        int AllCount = 0;

        for (int i = 0; i < Tmp.Tables[0].Rows.Count; i++)
        {
            SR = svTable.NewRow();

            SR["SuppliesId"] = Tmp.Tables[0].Rows[i]["SuppliesId"];
            SR["DetailsId"] = Tmp.Tables[0].Rows[i]["DetailsId"];
            SR["SuppliesYear"] = GetYearMonth(Tmp.Tables[0].Rows[i]["SuppliesYear"], Tmp.Tables[0].Rows[i]["SuppliesMonth"]);//
            SR["EmpId"] = Tmp.Tables[0].Rows[i]["SuppliesEmpId"];
            SR["EmpName"] = Tmp.Tables[0].Rows[i]["EmpName"];
            SR["DepartmentName"] = Tmp.Tables[0].Rows[i]["DepartmentName"];
            SR["SName"] = Tmp.Tables[0].Rows[i]["SName"];
            SR["SId"] = Tmp.Tables[0].Rows[i]["SId"];
            SR["SType"] = Tmp.Tables[0].Rows[i]["SType"];
            SR["Counts"] = Tmp.Tables[0].Rows[i]["Counts"];
            object Cbt = Tmp.Tables[0].Rows[i]["Counts"];
            int Ct = Convert.IsDBNull(Cbt) ? 0 : Convert.ToInt32(Cbt);
            AllCount = AllCount + Ct;
            SR["CCounts"] = Tmp.Tables[0].Rows[i]["CCounts"];

            object Cb = Tmp.Tables[0].Rows[i]["CCounts"];
            int Co = Convert.IsDBNull(Cb) ? 0 : Convert.ToInt32(Cb);

            SR["NCounts"] = (Co == 0) ? Ct : 0;
            SR["IsConfirm"] = Tmp.Tables[0].Rows[i]["IsConfirm"];//2008-11-6添加
            SR["UnitPrice"] = Tmp.Tables[0].Rows[i]["UnitPrice"];//2009-04-15新增
            svTable.Rows.Add(SR);
        }

        SR = svTable.NewRow();

        SR["SName"] = "合计";
        SR["Counts"] = AllCount;

        svTable.Rows.Add(SR);

        wgvMaster.DataSource = svTable;
        wgvMaster.DataBind();

    }

    private DataTable MakeNewWTable()
    {
        DataTable WTable = new DataTable("WMaster");

        WTable.Columns.Add(new DataColumn("SuppliesId", typeof(Int32)));
        WTable.Columns.Add(new DataColumn("DetailsId", typeof(Int32)));
        WTable.Columns.Add(new DataColumn("SuppliesYear", typeof(string)));
        WTable.Columns.Add(new DataColumn("EmpId", typeof(string)));
        WTable.Columns.Add(new DataColumn("EmpName", typeof(string)));
        WTable.Columns.Add(new DataColumn("DepartmentName", typeof(string)));
        WTable.Columns.Add(new DataColumn("SId", typeof(Int32)));
        WTable.Columns.Add(new DataColumn("SName", typeof(string)));
        WTable.Columns.Add(new DataColumn("SType", typeof(string)));
        WTable.Columns.Add(new DataColumn("Counts", typeof(Int32)));
        WTable.Columns.Add(new DataColumn("CCounts", typeof(Int32)));
        WTable.Columns.Add(new DataColumn("NCounts", typeof(Int32)));
        WTable.Columns.Add(new DataColumn("IsConfirm", typeof(Boolean)));//2008-11-6添加        
        WTable.Columns.Add(new DataColumn("UnitPrice", typeof(decimal)));//2009-04-15新增
        return WTable;
    }


    protected void wgvMaster_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        wgvMaster.PageIndex = e.NewPageIndex;
        NewDataSet();
    }

    protected void ButSave_Click(object sender, EventArgs e)
    {
        string SuppliesId = "";
        string DetailsId = "";
        string SId = "";
        string empId = "";
        int Count = 0;

        for (int i = 0; i < wgvMaster.Rows.Count; i++)
        {
            ZLTextBox ZLNC = (ZLTextBox)wgvMaster.Rows[i].FindControl("ZLNCounts");

            Label lblSuId = (Label)wgvMaster.Rows[i].FindControl("Label1");
            Label lblDId = (Label)wgvMaster.Rows[i].FindControl("Label2");
            Label lblSId = (Label)wgvMaster.Rows[i].FindControl("Label3");
            Label lblEmpId = (Label)wgvMaster.Rows[i].FindControl("Label4");

            Label UnitPriceLabel = (Label)wgvMaster.Rows[i].FindControl("UnitPriceLabel");

            if (lblSuId != null)
                SuppliesId = lblSuId.Text;
            if (lblDId != null)
                DetailsId = lblDId.Text;
            if (lblSId != null)
                SId = lblSId.Text;
            if (lblEmpId != null)
                empId = lblEmpId.Text;

            //SuppliesId = wgvMaster.Rows[i].Cells[0].Text;
            //DetailsId = wgvMaster.Rows[i].Cells[1].Text;
            //SId = wgvMaster.Rows[i].Cells[5].Text;
            string DNow = DateTime.Now.ToString();
            int SS = 0;
            int SD = 0;
            decimal UnitPrice = 0;

            if (int.TryParse(SuppliesId, out SS) == true)
                SS = int.Parse(SuppliesId);
            else
                SS = 0;
            if (int.TryParse(DetailsId, out SD) == true)
                SD = int.Parse(DetailsId);
            else
                SD = 0;
            if (decimal.TryParse(UnitPriceLabel.Text, out UnitPrice))
                UnitPrice = decimal.Parse(UnitPriceLabel.Text);
            else
                UnitPrice = 0;

            if (SS > 0 && SD > 0)
                if (ZLNC != null)
                {
                    string AddNC = ZLNC.Text;
                    int addCount = 0;
                    if (int.TryParse(AddNC, out addCount))
                    {
                        addCount = int.Parse(AddNC);
                        if (addCount != 0)
                        {
                            //AddNC = "-" + AddNC;
                            decimal CountMoney = addCount * UnitPrice;
                            string sql = string.Format("execute Services_OfficePullDown  {0},{1},{2},{3},{4},'{5}',{6} ", SId, SuppliesId, DetailsId, AddNC, CountMoney, DNow, empId);
                            CliUtils.ExecuteSql("GLModule", "cmdRefValUse", sql, true, CliUtils.fCurrentProject);
                            Count = Count + 1;
                        }
                    }
                }
        }

        NewDataSet();

        string message = "";
        message = @"完成了" + Count.ToString() + "行数据导入";
        ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert(\"" + message + "\");", true);
        //this.Page.Response.Write("<script>alert(\"" + message + "\");</script>");
    }
}
